package cn.edu.ldu.communityqa.communityqa.controller;

import cn.edu.ldu.communityqa.communityqa.entity.Answer;
import cn.edu.ldu.communityqa.communityqa.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/answer")
public class AnswerController {
    private final AnswerService answerService;

    @Autowired
    public AnswerController(AnswerService answerService) {
        this.answerService = answerService;
    }
    @GetMapping
    public List<Answer> findAll() {
        return answerService.findAll();
    }

    @GetMapping("/{id}")
    public Answer findById(@PathVariable Long id) {
        return answerService.findById(id);
    }

    @GetMapping("/question-id/{id}")
    public List<Answer> findByQuestionId(@PathVariable Long id) {
        return answerService.findByQuestionId(id);
    }

    @PostMapping
    public int save(@RequestBody Answer answer) {
        return answerService.save(answer);
    }

    @PutMapping
    public int update(@RequestBody Answer answer) {
        return answerService.update(answer);
    }

    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Long id) {
        return answerService.deleteById(id);
    }
}
